package com.outfood.service.impl;

import com.outfood.config.PythonServerConfig;
import lombok.extern.log4j.Log4j2;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfood-platform
 * @Package com.outfood.service.impl
 * @Date 2024-04-12 13:11
 * @Version 1.0
 */
@Service
@Log4j2
public class CreateElemeTokenService {

    private WebDriver webDriver;

    @Resource
    PythonServerConfig pythonServerConfig;

    @PostConstruct
    public WebDriver createLiunxWebDriver() throws IOException {
        System.setProperty("webdriver.chrome.driver",pythonServerConfig.getDriverUrl());
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        chromeOptions.setExperimentalOption("useAutomationExtension",false);
        chromeOptions.setExperimentalOption("excludeSwitches",new String[]{"enable-automation"});
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        this.webDriver = driver;
        webDriver.get("https://intelligent-api.huisonglin.top/getsign.html");
        log.info("已经链接到cookie服务器");
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        String sign = (String) js.executeScript("return etSign()");
        log.info("sign="+sign);
        return driver;
    }



    public String getsign(){
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        String sign = (String) js.executeScript("return etSign()");
        log.info("sign="+sign);
        return sign;
    }

    @Deprecated
    public void der(){
        webDriver.quit();
    }





}
